<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Event_Location extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by='created_date', $sort_order='asc', $current_page=0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					//echo '<pre>';print_r($search_by_value);exit;
					}
				}
			$results_per_page = 3;
		
		
			$data = array('www-command'=>'events-all_event_location', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order, 'search_by'=>$search_by, 'search_by_value'=>$search_by_value);
			$response = $this->get_event_list($data);
			//echo '<pre>';print_r($response);exit;
			
			if($response->message !== 'No event locations'){
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/event_location/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['events_all_event_location'] = $response;
			$data['location'] = '';
			}else{
				$data['location'] = 1;
				$data['events_all_event_location'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['sort_order'] = $sort_order;
			$data['order_by'] = $order_by;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$data['location_fields'] = array('location_name'=>'Name', 'location_address'=>'Address', 'location_city'=>'City', 'status'=>'Stutus');
			$data['status'] = array(1=>'Active', 0=>'In-active');
			$this->load->view('admin/event_location', $data);
 		}
		
		function search_by(){
			if($this->input->post('search') == ''){
				$search = $this->input->post('status');
			}else{
				$search = $this->input->post('search');	
			}
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/event_location/index/'.$query_id);
			
		}
		
		function create_event_location(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('address', 'Address', 'required');
				$this->form_validation->set_rules('city', 'City', 'required');
				$this->form_validation->set_rules('type', 'Type', 'required');
				
					if($this->form_validation->run() == FALSE){
						
				$data = array('www-command'=>'events-get_event_type_list');
				$get_event_type_list = $this->get_event_list($data);
				$data['get_event_type_list'] = $get_event_type_list;
				$this->load->view('admin/create_event_location_page', $data);
				}else{
					
				
				$data = array('www-command'=>'events-create_event_location', 
					'location_name' => $this->input->post('name'),
					'event_type_id' => $this->input->post('type'),
					'location_city' => $this->input->post('city'),
					'location_address' => $this->input->post('address')
					);
					$response = $this->get_event_list($data);
					//echo '<pre>';print_r($response);exit;
					redirect('admin/event_location/index/');
				}
			}
			
			 function get_event_list($data){
				$url = CURL_URL;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				return $response;
			}
			
			function edit_location(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('address', 'Address', 'required');
				$this->form_validation->set_rules('city', 'City', 'required');
				$this->form_validation->set_rules('type', 'Type');
				
				if($this->form_validation->run() == FALSE){
						if($this->uri->segment(4) > 0){
					$_SESSION['event_id'] = $this->uri->segment(4);
				}
					$data = array('www-command'=>'events-get_event_location', 
					'location_id' => $_SESSION['event_id']
					);
					$get_event_location = $this->get_event_list($data);
					
					$data = array('www-command'=>'events-get_event_type_list');
					$get_event_type = $this->get_event_list($data);
					$data['get_event_location'] = $get_event_location;
					$data['get_event_type'] = $get_event_type;
					//echo '<pre>';print_r($data['get_event_location']);exit;
					$this->load->view('admin/edit_event_location_page', $data);
			
					
				}else{
					$data = array('www-command'=>'events-edit_event_location', 
					'location_name' => $this->input->post('name'),
					'event_type_id' => $this->input->post('type'),
					'location_city' => $this->input->post('city'),
					'location_address' => $this->input->post('address'),
					'location_id' => $_SESSION['event_id']
					);
					//echo '<pre>';print_r($data);exit;
					$response = $this->get_event_list($data);
					//echo '<pre>';print_r($response);exit;
					redirect('admin/event_location/index/');
				}
			}
			
				function delete_event_location(){
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'events-delete_event_location','status'=>$status, 'location_id'=>$this->uri->segment(4));
				$this->get_event_list($data);
				//echo '<pre>';print_r($response);exit;
				redirect('admin/event_location/index/');
			}
}